package com.kele.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kele.domain.User;
import com.kele.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 赵奥林
 * @date 2021年05月27日 10:04
 * @Description  管理模块：
 *                      1.管理员对用户的操作（CRUD）
 */
@Controller
@RequestMapping("/manager")
public class ManagerController {


    @Resource
    private UserService userService;


    /**
      *@author 赵奥林
      *@description 2021/5/27 23:21管理员查询所有用户信息
     * @param model
     * @return java.lang.String
      */
    @RequestMapping(value = "/findAllUser",method = RequestMethod.GET)
    public String findAllUser(Model model,
                              @RequestParam(value = "pageNums",defaultValue = "1") int pageNums){
        PageHelper.startPage(pageNums,5);
        List<User> list=userService.findAll();
        PageInfo pageInfo = new PageInfo(list,5);
        model.addAttribute("pageInfo",pageInfo);

        return "static_resources/templates/management";
    }


    /**
      *@author 赵奥林
      *@description 2021/5/27 23:22 //跳转到修改页面
     * @param id
     * @param model
     * @return java.lang.String
      */
    @RequestMapping(value = "/toUpdateUsers")
    public String toUpdateUsers(int id,Model model){
        User user = userService.queryUserById(id);
        model.addAttribute("user",user);
        return "/static_resources/templates/updateUsers";
    }

    /**
      *@author 赵奥林
      *@description 2021/5/27 23:22 //管理员修改用户信息
     * @param user
     * @return java.lang.String
      */
    @RequestMapping(value = "/updateUsers",method = RequestMethod.POST)
    public String updateUsers(User user,int pageNum){
       userService.updateUser(user);
        System.out.println(pageNum);
       return "redirect:/manager/findAllUser?pageNums="+pageNum;
    }



    /**
      *@author 赵奥林
      *@description 2021/5/27 23:22 //管理员删除用户
     * @param id
     * @return java.lang.String
      */
    @RequestMapping(value = "/deleteUser",method = RequestMethod.GET)
    public String deleteUser(int id,int pageNum,int pageSize){
       userService.deleteUser(id);
        System.out.println(pageNum);
        System.out.println(pageSize);
        if (pageSize-1<=0){
            pageNum=pageNum-1;
        }
        if (pageNum-1<=0){
            pageNum=1;
        }
       return "redirect:/manager/findAllUser?pageNums="+pageNum;
    }
}
